package com.yixincaipiao.demos;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;


import com.yixincaipiao.R;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2017/4/26.
 */

public class ListDataAdapter extends RecyclerView.Adapter<ListDataAdapter.MyHolder> {
    private Context mContext;
    private List<String> dataList = new ArrayList<>();

    public ListDataAdapter(Context mContext) {
        this.mContext = mContext;
    }

    public void setData(List<String> strList, boolean isFirst) {
        if (isFirst) {
            dataList.clear();
        }
        dataList.addAll(strList);
        notifyDataSetChanged();
    }

    @Override
    public MyHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View v = LayoutInflater.from(mContext).inflate(R.layout.demo_list_adapter_layout, null);
        return new MyHolder(v);
    }

    @Override
    public void onBindViewHolder(MyHolder holder, int position) {
        holder.tv.setText(dataList.get(position));
    }

    @Override
    public int getItemCount() {
        return dataList!=null?dataList.size():0;
    }

    public class MyHolder extends RecyclerView.ViewHolder {
        public TextView tv;

        public MyHolder(View itemView) {
            super(itemView);
            tv = (TextView) itemView.findViewById(R.id.textview);
        }
    }
}
